package cn.sxt.clinic.controller;


import cn.sxt.clinic.pojo.*;
import cn.sxt.clinic.service.ISupplierService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2020-06-29
 */
@Controller
@RequestMapping("/supplier")
public class SupplierController {

    @Autowired
    private ISupplierService supplierService;

    @RequiresPermissions("supplier:supplierManager")
    @RequestMapping("/supplierManager")
    public String supplierManager(){
        return "supplierManager";
    }

    @RequiresPermissions("supplier:list")
    @RequestMapping("/list")
    @ResponseBody
    public ResultObject list(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10")Integer pageSize,
                             String keyword){

        //分页对象
        Page<Supplier> page = new Page<>(pageNum, pageSize);
        //条件对象
        QueryWrapper<Supplier> queryWrapper = new QueryWrapper<>();

        if (StringUtils.isNotBlank(keyword)){
            //根据挂号类型模糊查询
            queryWrapper.like("supplier",keyword);

            queryWrapper.or();
            queryWrapper.like("supplier_contact",keyword);
        }

        //查询结果
        Page<Supplier> result = supplierService.page(page,queryWrapper);
        return new ResultObject(result);
    }

    @RequiresPermissions("supplier:delete")
    @RequestMapping("/delete")
    @ResponseBody
    public MessageObject delete(long supplierId){

        boolean flag = supplierService.removeById(supplierId);

        MessageObject mo = new MessageObject(1, "删除数据成功");
        if (!flag){
            mo = new MessageObject(0, "删除数据失败,请联系管理员");
        }
        return mo;
    }

    @RequestMapping("/supplierEdit")
    public String supplierEdit(Model m, Long supplierId){
        //如果id为空，添加，为空，修改
        if (supplierId != null){
            Supplier supplier = supplierService.getById(supplierId);
            m.addAttribute("supplier",supplier);
        }

//        QueryWrapper<Dictionary> wrapper = new QueryWrapper<>();
//        wrapper.eq("parent_id",46);
//
//        List<Dictionary> medicineType = dictionaryService.list(wrapper);
//
//        m.addAttribute("medicineType",medicineType);
//
//        QueryWrapper<Dictionary> wrapper1 = new QueryWrapper<>();
//        wrapper1.eq("parent_id",38);
//
//        List<Dictionary> mincineUint = dictionaryService.list(wrapper1);
//
//        m.addAttribute("mincineUint",mincineUint);
//
//        QueryWrapper<Dictionary> wrapper2 = new QueryWrapper<>();
//        wrapper2.eq("parent_id",3);
//
//        List<Dictionary> medicineSpecs = dictionaryService.list(wrapper2);
//
//        m.addAttribute("medicineSpecs",medicineSpecs);

        return "supplierEdit";
    }

    @RequiresPermissions("supplier:insert")
    @RequestMapping("/insert")
    @ResponseBody
    public MessageObject insert(Supplier supplier) {

        supplier.setCreateDate(LocalDate.now());

        boolean flag = supplierService.saveOrUpdate(supplier);

        MessageObject mo = new MessageObject(1, "新增数据成功");
        if (!flag) {
            mo = new MessageObject(0, "新增数据失败,请联系管理员");
        }
        return mo;
    }

    @RequiresPermissions("supplier:update")
    @RequestMapping("/update")
    @ResponseBody
    public MessageObject update(Supplier supplier) {

        boolean flag = supplierService.saveOrUpdate(supplier);

        MessageObject mo = new MessageObject(1, "修改数据成功");
        if (!flag) {
            mo = new MessageObject(0, "修改数据失败,请联系管理员");
        }
        return mo;
    }

}

